package com.fanqi.ipcapp;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

//不同两个app的通信
public class TwoAppIPCService extends Service {

    private CopyOnWriteArrayList<Book> books = new CopyOnWriteArrayList<>();

    private Callback callback;


    private Binder mBinder = new IBookManager.Stub() {
        @Override
        public List<Book> getBooks() throws RemoteException {
            return books;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            book.setName(book.getName() + "经过服务");
            books.add(book);

            //TODO 为了效果添加
            //同步广播，这里的广播只是为了在客户端发送一个信息到服务时，服务这边的UI也更新。这里为了快速见效，使用广播，真实使用，请酌情考虑
            Intent broacastIntent = new Intent();
            broacastIntent.setAction("com.fanqi.ipc.b");
            sendBroadcast(broacastIntent);

            //并启动activity


        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }


}
